<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        offset="50"
      >
        <van-cell v-for="item in list" :key="item" :title="item" />

        <!-- 文章列表 -->
        <articleItem
          v-for="obj in articlesList"
          :key="obj.id"
          :artObj="obj"
          @disLikeEV="dislikeFn"
          @reports="reportsFn"
          @click.native="$router.push(`/detail?art_id=${obj.art_id}`)"
        ></articleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from '../../../components/ArticleItem.vue'
import { articlesListAPI, articleDislikeAPI, articleReportsAPI } from '@/api'

import Notify from '@/ui/Notify'
export default {
  data () {
    return {
      articlesList: [],
      finished: false,
      loading: false,
      list: [],
      theTime: new Date().getTime(),
      isLoading: false
    }
  },
  props: {
    channelId: Number
  },
  components: {
    ArticleItem
  },
  async created () {
    if (this.articlesList.length === 0) {
      this.loading = false
      return
    }
    const res = await articlesListAPI({
      channelId: this.channelId, // 默认推荐频道(id为0)
      timestamp: this.theTime
    })
    // console.log(res)
    this.articlesList = res.data.data.results
    this.theTime = res.data.data.pre_timestamp
  },
  methods: {
    // 触底加载更多
    async onLoad () {
      const res = await articlesListAPI({
        channelId: this.channelId, // 默认推荐频道(id为0)
        timestamp: this.theTime
      })
      // console.log(res)
      if (res.data.data.results.length === 0) {
        this.finished = true
        return
      }
      this.articlesList = [...this.articlesList, ...res.data.data.results]
      this.theTime = res.data.data.pre_timestamp
      this.loading = false
      if (res.data.data.pre_timestamp === null) {
        this.finished = true
      }
    },
    // 下拉刷新
    async onRefresh () {
      this.articlesList = []
      this.theTime = new Date().getTime()
      const res = await articlesListAPI({
        channelId: this.channelId, // 默认推荐频道(id为0)
        timestamp: this.theTime
      })
      // console.log(res)
      if (res.data.data.results.length === 0) {
        this.finished = true
        return
      }
      this.articlesList = [...this.articlesList, ...res.data.data.results]
      this.theTime = res.data.data.pre_timestamp
      this.isLoading = false
    },
    async dislikeFn (id) {
      // -反馈- 不感兴趣
      try {
        await articleDislikeAPI({
          artId: id
        })
        Notify({ type: 'success', message: '反馈成功' })
      } catch (err) {
        Notify({ type: 'warning', message: '反馈失败' })
      }
    },
    async reportsFn (id, type) {
      // -反馈- 垃圾内容
      try {
        await articleReportsAPI({
          target: id,
          type: type,
          remark: '就是其他问题'
        })
        Notify({ type: 'success', message: '举报成功' })
      } catch (err) {
        Notify({ type: 'warning', message: '举报失败' })
      }
    }
  }
}
</script>
<style scoped lang="less">
</style>
